package conversion;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * przyklady requestow
 * 
 * otto.ucns.uga.edu - - [01/Jul/1995:10:20:41 -0400] "GET /shuttle/missions/61-b/mission-61-b.html HTTP/1.0" 200 6503
 * 131.182.28.171 - - [01/Jul/1995:10:20:33 -0400] "GET /history/gemini/gemini-v/gemini-v-patch-small.gif HTTP/1.0" 200 19150
 * slip183-11.kw.jp.ibm.net - - [01/Jul/1995:10:20:35 -0400] "GET /history/mercury/mercury.html HTTP/1.0" 200 1871
 * www-a2.proxy.aol.com - - [01/Jul/1995:10:20:37 -0400] "GET / HTTP/1.0" 200 7074
 * ix-akr-oh2-10.ix.netcom.com - - [01/Jul/1995:10:20:41 -0400] "GET /software/winvn/winvn.gif HTTP/1.0" 200 25218
 */
public class Request {
	public static String REQUEST_PATTERN =
	// "^(.*?) - - \\[(.*) -0400\\] \"GET (.*) HTTP/1.0\" (\\d*) (\\d*)$";
		"^(.*?) - - \\[(.+)\\] \"(GET|POST) (.+) HTTP/1.0\" (\\d+) (\\d+)$";

	private static final Pattern COMPILED_REQUEST_PATTERN =
		Pattern.compile(REQUEST_PATTERN);

	private final String wholeRequest;
	private final String status;
	private final String resource;
	private final String requesterIP;
	private final String date;
	private final String method;
	private final String bytesSent;

	private Request(Matcher m) {
		this.wholeRequest = m.group(0);
		this.requesterIP = m.group(1);
		this.date = m.group(2);
		this.method = m.group(3);
		this.resource = m.group(4);
		this.status = m.group(5);
		this.bytesSent = m.group(6);
	}

	public Calendar getTime() {
		// przyklad: 01/Jul/1995:10:20:41
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss Z").parse(this.getNumberizedDate()));
		} catch (ParseException e) {
			throw new Error(e);
		}
		return cal;
	}
	

	/**
	 * @return the requesterIP
	 */
	public String getRequesterIP() {
		return requesterIP;
	}

	/**
	 * @return the wholeRequest
	 */
	public String getWholeRequest() {
		return wholeRequest;
	}

	/**
	 * @return the resource
	 */
	public String getResource() {
		return resource;
	}

	/**
	 * @return the date
	 */
	public String getDate() {
		return date;
	}

	/**
	 * @return the bytesSent
	 */
	public String getBytesSent() {
		return bytesSent;
	}

	public static void main(String[] args) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss Z").parse("01/04/1995:10:20:37 -0400"));
		System.out.println(cal);
	}

	private String getNumberizedDate() {
		return this.date.replaceAll("Aug", "08").replaceAll("Jul", "07");
	}

	public boolean isResourceHtmlPage() {
		return this.resource.equals("/") || this.resource.endsWith(".html")
			|| this.resource.endsWith(".htm");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.wholeRequest;
	}

	/**
	 * @return the status
	 */
	public int getStatus() {
		return Integer.parseInt(status);
	}

	/**
	 * @return the method
	 */
	public String getMethod() {
		return method;
	}

	public static boolean isRequest(String line) {
		return line.matches(REQUEST_PATTERN);
	}

	public static Request createRequest(String line) {
		Matcher m = COMPILED_REQUEST_PATTERN.matcher(line);
		if (m.find())
			return new Request(m);
		else
			return null;
	}
}
